//
//  TTGuidePageViewController.m
//  TTBaseProject
//
//  Created by shny588 on 16/7/21.
//  Copyright © 2016年 tt. All rights reserved.
//

#import "TTGuidePageViewController.h"
#import "TTInitUI.h"

@interface TTGuidePageViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) NSArray           *guidePageArray;
@property (nonatomic, strong) UIScrollView      *guidePageScrollView;
@property (nonatomic, strong) UIButton          *gotoLogin;

@end

@implementation TTGuidePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initView];
}

- (void)initView {
    _guidePageArray = @[@"guide_page1", @"guide_page2"];
    
    _guidePageScrollView = [[UIScrollView alloc] init];
    _guidePageScrollView.frame = CGRectMake(0, 0, [TTDeviceInfo screenWidth], [TTDeviceInfo screenHeight]);
    _guidePageScrollView.contentSize = CGSizeMake([TTDeviceInfo screenWidth] * _guidePageArray.count, [TTDeviceInfo screenHeight]);
    _guidePageScrollView.pagingEnabled = YES;
    _guidePageScrollView.bounces = NO;
    _guidePageScrollView.showsHorizontalScrollIndicator = NO;
    _guidePageScrollView.delegate = self;
    [self.view addSubview:_guidePageScrollView];
    
    for (int i = 0; i < _guidePageArray.count; i++)
    {
        UIImageView *guideImageView = [[UIImageView alloc] init];
        guideImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",_guidePageArray[i]]];
        guideImageView.frame = CGRectMake([TTDeviceInfo screenWidth] * i, 0, [TTDeviceInfo screenWidth], [TTDeviceInfo screenHeight]);
        [_guidePageScrollView addSubview:guideImageView];
    }
    
    //点击事件
    _gotoLogin = [UIButton buttonWithType:UIButtonTypeCustom];
    _gotoLogin.backgroundColor = [UIColor clearColor];
    [_gotoLogin setBackgroundImage:[UIImage imageNamed:@"experience"] forState:UIControlStateNormal];
    [_gotoLogin setBackgroundImage:[UIImage imageNamed:@"experience_H"] forState:UIControlStateHighlighted];
    [_gotoLogin addTarget:self action:@selector(delayOperation) forControlEvents:UIControlEventTouchUpInside];
    _gotoLogin.adjustsImageWhenHighlighted = NO;
    _gotoLogin.hidden = YES;
    [_guidePageScrollView addSubview:_gotoLogin];
    
    [_gotoLogin mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.equalTo(self.view.mas_left).offset(70);
         make.right.equalTo(self.view.mas_right).offset(-70);
         make.bottom.equalTo(self.view.mas_bottom).offset(-50);
         make.height.equalTo(@(45 * ([TTDeviceInfo screenWidth] - 140) / 180));
     }];
}

//拖动前的起始坐标
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == [TTDeviceInfo screenWidth] * (_guidePageArray.count - 1))
    {
        _gotoLogin.hidden = NO;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == [TTDeviceInfo screenWidth] * (_guidePageArray.count - 1))
    {
        _gotoLogin.hidden = NO;
    }
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    _gotoLogin.hidden = YES;
}


- (void)delayOperation
{
    [[TTInitUI shared] initUI:nil];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
